package sdfi.lee.Listener;

import javax.jms.Connection;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import sdfi.lee.JMS.ConnectionFactory;
import sdfi.lee.Logger.MyLogger;
import sdfi.lee.SetFileToDB.DealwithProfile;

/**
 * Application Lifecycle Listener implementation class ReceiveProfileAndSgmlListener
 *
 */
public class ReceiveProfileAndSgmlListener implements ServletContextListener {

    /**
     * Default constructor. 
     */
    public ReceiveProfileAndSgmlListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    	MyLogger.Logger("启动监听器,监听mysql队列,处理Profile and SGML", null, "info");
    	this.start();
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    	
    }
    private void start()
    {

		Session session=null;
		MessageConsumer consumer=null;
		Connection con=ConnectionFactory.getCon();
		String queue="mysql";
		try
		{
		con.start();
		session=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		consumer=session.createConsumer(session.createQueue(queue));
		consumer.setMessageListener(new MessageListener(){
			public void onMessage(Message message)
			{
				
					
					MapMessage mapmsg=(MapMessage)message;
					String value="";
					try
					{
						Object obj=mapmsg.getMapNames().nextElement();
						value=mapmsg.getString(obj.toString());
						MyLogger.Logger(value, null, "info");
					}
					catch(Exception ee)
					{
						ee.printStackTrace();
					}
					if(!"".equals(value)&&null!=value)
					{
						
						if(value.startsWith("file"))//处理SGML
						{
							
							String sign=value.substring(4,5);
							String path=value.substring(value.indexOf(sign)+1);
						
							String type="others";
							if(value.contains("patent"))
								type="patent";
							else if(value.contains("policy"))
								type="policy";
							else if(value.contains("journal"))
								type="journals";
							else if(value.contains("notice"))
								type="notice";
							else if(value.contains("achievement"))
								type="achievement";
							else if(value.contains("inner"))
								type="inner";
							else if(value.contains("outer"))
								type="outer";
							String operate="delete";
							if("+".equals(sign))
								operate="add";
							if(path==null||"".equals(path)) return ;
							//path=path.replace("\\", "\\\\");
							DealwithProfile.setToSMGLDB(path, type, operate);
							
						}
						else//处理profile
						{
							String regex="\\s+";
							String s[]=value.split(regex);
							if(s.length>0)
							{
								DealwithProfile.setProfileToDB(s[1], s[0]);
							}
							
						}
						
					}
					
				
			}
		});
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
		
    }
    
	
}
